package com.ruiya.staging.goods.locate.service;

import com.ruiya.staging.goods.core.constants.ErrorCodeEnum;
import com.ruiya.staging.goods.core.model.GoodsIdVO;
import com.ruiya.staging.goods.data.grpc.*;
import com.ruiya.staging.goods.locate.client.DataIOClient;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Deprecated
@Service
public class TestService {
    private static final Logger _log = LoggerFactory.getLogger(TestService.class);

    @Autowired
    private DataIOClient dataIOClient;

    public Pair<ErrorCodeEnum, GoodsIdVO> locateGoodsId(String tid, String goodsNo) {
        LocateGoodsIdRequest request = LocateGoodsIdRequest.newBuilder()
                .setUri(DataURI.newBuilder()
                        .setTid(tid)
                        .setSharding("none")
                        .build())
                .setGoodsNo(goodsNo)
                .build();

        LocateGoodsIdResponse grpcResponse = this.dataIOClient.locateGoodsId(request);
        if (grpcResponse.getStatus().getCode() != RpcStatus.Codes.OK_VALUE) {
            _log.warn("tid={}, rpcStatus={}, rpcMemo={}", tid, grpcResponse.getStatus().getCode(), grpcResponse.getStatus().getMemo());
            return new ImmutablePair<>(ErrorCodeEnum.DATAIOERROR, null);
        }

        GoodsIdVO goodsIdVO = new GoodsIdVO(grpcResponse.getGoodsId());
        return new ImmutablePair<>(ErrorCodeEnum.OK, goodsIdVO);
    }

}
